package com.rzjm.exercise.sys.core.file.service.impl;

import com.rzjm.exercise.common.base.service.impl.BaseServiceImpl;
import com.rzjm.exercise.common.config.ProjectUrlConfig;
import com.rzjm.exercise.common.utils.StringUtils;
import com.rzjm.exercise.sys.core.file.dao.FileDao;
import com.rzjm.exercise.sys.core.file.domain.FileDO;
import com.rzjm.exercise.sys.core.file.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;
import java.util.Map;



@Service
public class FileServiceImpl extends BaseServiceImpl<FileDao, FileDO> implements FileService {
	@Resource
	private FileDao sysFileMapper;
	@Resource
	private ProjectUrlConfig projectUrlConfig;

	@Override
	public List<FileDO> list(Map<String, Object> map){
		List<FileDO> list = sysFileMapper.selectByMap(map);
		if(list!=null && list.size() > 0){
			for (FileDO file : list){
				file.setUrl(projectUrlConfig.getImgurl() + file.getUrl());
			}
		}
		return list;
	}

	@Override
	public List<FileDO> selectByColumn(String column, String value, String filePrefix) {
		List<FileDO> result = this.baseDao.selectListByColumn(column, value);
		if (StringUtils.isBlank(filePrefix)) {
			return result;
		}
		result.forEach(t -> {
			t.setUrl(filePrefix + t.getUrl());
		});
		return result;
	}

    @Override
    public Boolean isExist(String url) {
		Boolean isExist = false;
		if (!StringUtils.isEmpty(url)) {
			String filePath = url.replace("/files/", "");
			filePath = projectUrlConfig.getUploadPath() + filePath;
			File file = new File(filePath);
			if (file.exists()) {
				isExist = true;
			}
		}
		return isExist;
	}
}
